package cn.youframe.frame.util;

import cn.youframe.frame.base.YFBase;
import cn.youframe.frame.beans.CommTypeValVO;
import cn.youframe.frame.cache.CacheMgr;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;

/**
 * User: Administrator
 * DateTime: 12-4-25: 下午3:31
 */
public class ToolsUtil implements YFBase {
    private static CacheMgr manager= CacheMgr.getInstance();

    /**
     * 获取字典表类型
     * @param typeId
     * @return
     */
    public static  Map<String, CommTypeValVO> getDictType(String typeId){
        return (Map<String, CommTypeValVO>)manager.get(typeId);
    }
    /**
     * getValueName 根据类型编号和值编号获得值名称
     *
     * @param typeID String
     * @param valueID String
     * @return String
     */
    public static String getDictValue(String typeID, String valueID) {
        String valName=null;
        try {
            if (valueID == null) {
                return valName;
            }
            Map<String, CommTypeValVO> valMap = (Map<String, CommTypeValVO>)manager.get(typeID);
            if (valMap != null) {
                valName = valMap.get(valueID)==null?null:valMap.get(valueID).getValVal();
            }
        } catch (Exception ex) {
            logger.error("根据类型编号和值编号获得值异常：",ex);
        }
        return valName;
    }
    /**
     * getNameValue 根据类型编号和值获得值编号
     *
     * @param typeID String
     * @param val String
     * @return String
     */
    public static String getValueId(String typeID, String val) {
        String valID=null;
        try {
            Map<String, CommTypeValVO> valMap = (Map<String, CommTypeValVO>)manager.get(typeID);
            if(valMap!=null){
                Iterator<Map.Entry<String, CommTypeValVO>> valItr=valMap.entrySet().iterator();
                CommTypeValVO valVO=null;
                while(valItr.hasNext()){
                    valVO=valItr.next().getValue();
                    if(valVO.getValVal().equals(val)){
                        valID=valVO.getValId();
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("根据类型编号和值名称获得值编号异常：",ex);
        }
        return valID;
    }
    /**
     * getValueVO 根据类型编号和值编号获得值的VO对象
     *
     * @param typeID String
     * @param valueID String
     * @return CommTypeValVO
     */
    public static CommTypeValVO getValueVO(String typeID, String valueID){
        CommTypeValVO valVO=null;
        try {
            Map<String, CommTypeValVO> valMap = (Map<String, CommTypeValVO>)manager.get(typeID);
            if (valMap != null) {
                valVO = valMap.get(valueID);
            }
        } catch (Exception ex) {
            logger.error("根据类型编号和值编号获得值的VO对象异常：",ex);
        }
        return valVO;
    }

    /**
     * 获取客户端IP地址，此方法用在proxy环境中
     * @param request
     * @return
     */
    public static String getRemoteAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotBlank(ip)) {
            String[] ips = StringUtils.split(ip, ',');
            if (ips != null) {
                for (String tmpip : ips) {
                    if (StringUtils.isBlank(tmpip))
                        continue;
                    tmpip = tmpip.trim();
                    if (isIPAddr(tmpip) && !"127.0.0.1".equals(tmpip)) {
                        return tmpip.trim();
                    }
                }
            }
        }
        ip = request.getHeader("x-real-ip");
        if (isIPAddr(ip))
            return ip;
        ip = request.getRemoteAddr();
        if (isIPAddr(ip))
            return ip;
        return "127.0.0.1";
    }
    /**
     * 判断字符串是否是一个IP地址
     * @param addr
     * @return
     */
    public static boolean isIPAddr(String addr) {
        if (StringUtils.isEmpty(addr))
            return false;
        String[] ips = StringUtils.split(addr, '.');
        if (ips.length != 4)
            return false;
        try {
            int ipa = Integer.parseInt(ips[0]);
            int ipb = Integer.parseInt(ips[1]);
            int ipc = Integer.parseInt(ips[2]);
            int ipd = Integer.parseInt(ips[3]);
            return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0
                    && ipc <= 255 && ipd >= 0 && ipd <= 255;
        } catch (Exception e) {
            logger.error("判断IP异常",e);
            return false;
        }
    }}
